package com.red.wine.adapter;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.red.wine.R;
import com.red.wine.model.TasteReturnData;
import com.red.wine.model.TasteInfo;
import com.red.wine.model.TopInfo;
import com.red.wine.view.ZoomImageView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;

public class GalleryAdapter extends BaseAdapter {

	private Context context;
	private TopInfo datasource;

	public GalleryAdapter(Context context, TopInfo datasource) {
		this.context = context;
		this.datasource = datasource;
	}
	public GalleryAdapter(Context context) {
		this.context = context;
	}

	public int getCount() {
		return datasource.getList().size();
	}

	public Object getItem(int position) {
		return datasource.getList().get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		TasteInfo taskInfo = datasource.getList().get(position);
//		if (taskInfo.getThumb() != null && taskInfo.getThumb().length > 0) {
//			for (int i = 0; i < taskInfo.getThumb().length; i++) {
////				Log.e("TAG", "imgurl = " + taskInfo.getThumb()[i]);
//				if(downBItmap == null){
//					Log.e("TAG", "gallery 下载图");
//					downImg(taskInfo.getThumb()[i]);
//				}else{
//					break;
//				}
//			}
// 		 }
		ZoomImageView view = new ZoomImageView(context, downBItmap.getWidth(), downBItmap.getHeight());
		view.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		view.setImageBitmap(downBItmap);
		return view;
	}
	private Bitmap downBItmap;
	private void downImg(String imgurl) {
		// 启动一个后台线程
		// 这里下载数据
		try {
			URL url = new URL(imgurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			int length = (int) conn.getContentLength();
//			Log.e("TAG", "length:= " + length);
			 
			if (length != -1) {
				byte[] imgData = new byte[length];
				byte[] buffer = new byte[length];
				int readLen = 0;
				int destPos = 0;
				ByteArrayOutputStream bis = new ByteArrayOutputStream(); 
				while ((readLen = is.read(buffer)) > 0) {
						bis.write(buffer, 0, readLen); 
				}
			 byte[] bb = bis.toByteArray();
			 downBItmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);
			}
			 Log.e("TAG", "downBItmap:= " + downBItmap.getWidth());
			is.close();
			conn.disconnect();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
